package cn.initcap.concurrency.concurrency.unsafe;

import cn.initcap.concurrency.concurrency.ThreadPoolCreatHelper;
import lombok.extern.slf4j.Slf4j;

/**
 * @author initcap
 * @date Created in 2018/6/16 PM12:09.
 */
@Slf4j
public class StringExample1 {

    /**
     * 总请求数
     */
    private static final int CLIENT_TOTAL = 5000;

    /**
     * 同时并发执行的线程数
     */
    private static final int THREAD_TOTAL = 200;

    /**
     * StirngBuilder线程不安全的类，不考虑多线程时，推荐优先选择
     */
    private static StringBuilder stringBuilder = new StringBuilder();

    /**
     * StringBuffer线程安全的类，内部使用了synchornaized关键字，性能会有些下降
     */
    private static StringBuffer stringBuffer = new StringBuffer();

    public static void update() {
        stringBuilder.append("1");
        stringBuffer.append("1");
    }

    public static void main(String[] args) throws Exception {
        ThreadPoolCreatHelper.threadPoolRun(CLIENT_TOTAL, THREAD_TOTAL,
                StringExample1.class, "update",
                "StringExample1");
        log.info("StringBuilder -> size:{}", stringBuilder.length());
        log.info("StringBuffer -> size:{}", stringBuffer.length());
    }

}
